<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="关键字" prop="productNum">
        <el-input v-model="queryParams.productNum" placeholder="请输入搜索内容" clearable size="small" style="width: 240px"
          @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="上传状态" prop="state">
        <el-select v-model="queryParams.state" placeholder="上传状态" clearable size="small" style="width: 240px">
          <el-option v-for="dict in statusOptions" :key="dict.dictValue" :label="dict.dictLabel"
            :value="dict.dictValue" />
        </el-select>
      </el-form-item>
      <el-form-item label="上传时间">
        <el-date-picker v-model="dateRange" size="small" style="width: 240px" value-format="yyyy-MM-dd" type="daterange"
          range-separator="-" start-placeholder="开始日期" end-placeholder="结束日期"></el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="cyan" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="danger" icon="el-icon-delete" size="mini" :disabled="multiple" @click="handleDelete"
          >删除</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getProductList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="productList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="50" align="center" />
      <el-table-column label="上传编号" align="center" prop="id" width="180" />
      <el-table-column label="文件名" align="center" prop="fileName"  :show-overflow-tooltip="true" />
      <el-table-column label="成功数" align="center" prop="successCount" width="140" />
      <el-table-column label="失败数" align="center" prop="failCount" width="140" />
      <el-table-column label="处理状态" align="center" prop="status" width="140" :formatter="statusFormat"/>
      <el-table-column label="创建时间" align="center" prop="uploadTime" width="160">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.uploadTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="400">
        <template slot-scope="scope">
          <el-button type="primary" plain @click="handleUpdate(scope.row)" >前往数据仓库发布</el-button>
          <el-button type="primary" plain @click="handleDetails(scope.row)" >查看解析详情</el-button>
          <el-button type="danger" plain @click="handleDelete(scope.row)" >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.currentPage" :limit.sync="queryParams.pageSize"
      @pagination="getProductList" />


      <detailsDialog  v-if="detailsStatus" :productId="productId" @closeDialog="detailsStatus = false" />


  </div>
</template>

<script>
import {
  productList,
  getProductInfo,
  delProductInfo,
  addProductInfo,
  updateProductInfo,
  dataPackageList,
  deleteData
} from "@/api/product/productInfo";
import { getToken } from "@/utils/auth";
import Treeselect from "@riophae/vue-treeselect";
import "@riophae/vue-treeselect/dist/vue-treeselect.css";
import detailsDialog from "./components/detailsDialog.vue";


export default {
  name: "product",
  components: { Treeselect, detailsDialog },
  data() {
    return {
      productId: null,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 产品表格数据
      productList: null,
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 日期范围
      dateRange: [],
      // 状态数据字典
      statusOptions: [],
      // 产品类型数据字典
      productTypeOptions: [],
      // 金额类型数据字典
      unitTypeOptions: [],
      // 表单参数
      form: {
      },
      defaultProps: {
        children: "children",
        label: "label",
      },
      // 查询参数
      queryParams: {
        currentPage: 1,
        pageSize: 10,
        productNum: undefined,
        productName: undefined,
        productType: undefined,
        state: undefined,
        tradeFace: undefined
      },
      // 表单校验
      rules: {
        productNum: [
          { required: true, message: "产品编码不能为空", trigger: "blur" },
        ],
        productName: [
          { required: true, message: "产品名称不能为空", trigger: "blur" },
        ],
        productType: [
          { required: true, message: "产品类型不能为空", trigger: "blur" },
        ],
        tradeFace: [
          {
            required: true,
            message: "请输入产品金额",
            trigger: "change"
          },
          {
            pattern: /^(([1-9]{1}\d*)|(0{1}))(\.\d{1,2})?$/,
            message: "请输入合法的金额数字，最多两位小数",
            trigger: "change"
          }
        ],
        unit: [
          { required: true, message: "金额单位不能为空", trigger: "blur" },
        ],
      },
      show: false,
      detailsStatus: false,
    };
  },

  created() {
    this.getProductList();
    this.getDicts("task_status").then((response) => {
      this.statusOptions = response.data;
    });
    this.getDicts("product_type").then((response) => {
      this.productTypeOptions = response.data;
    });
    this.getDicts("unit_type").then((response) => {
      this.unitTypeOptions = response.data;
    });
  },
  methods: {
    /** 查询产品列表 */
    getProductList() {
      this.loading = true;
      dataPackageList(this.addDateRange(this.queryParams, this.dateRange)).then(
        (response) => {
          this.productList = response?.value?.list || [];
          this.total = response?.value?.total || 0;
          this.loading = false;
        }
      );
    },
    // 产品状态修改
    handleStatusChange(row) {
      let text = row.state === 0 ? "启用" : "停用";
      this.$confirm(
        '确认要"' + text + '""' + row.productNum + '"产品吗?',
        "警告",
        {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
        }
      )
        .then(function () {
          return changeProductStatus(row.id, row.state);
        })
        .then(() => {
          this.msgSuccess(text + "成功");
        })
        .catch(function () {
          row.state = row.state == 0 ? 1 : 0;
        });
    },
    // 产品状态字典翻译
    statusFormat(row, column) {
      return this.selectDictLabel(this.statusOptions, row.status);
    },
    // 产品类型字典翻译
    productTypeFormat(row, column) {
      return this.selectDictLabel(this.productTypeOptions, row.productType);
    },
    // 金额类型字典防疫
    unitTypeFormat(row, column) {
      return this.selectDictLabel(this.unitTypeOptions, row.unit);
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: undefined,
        productNum: undefined,
        productType: undefined,
        tradeFace: undefined,
        state: '0',
        unit: undefined,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.page = 1;
      this.getProductList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.dateRange = [];
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.id);
      this.single = selection.length != 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加产品";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.$router.push('/dataDeploy/store')
      // this.reset();
      // const id = row.id || this.ids;
      // getProductInfo(id).then((response) => {
      //   this.form = response.data;
      //   this.form.state = this.form.state + '';
      //   this.form.state = this.form.state == null ? "" : this.form.state + '';
      //   this.form.productType = this.form.productType == null ? "" : this.form.productType + '';
      //   this.form.unit = this.form.unit == null ? "" : this.form.unit + '';
      //   this.form.tradeFace = this.form.tradeFace == null ? "" : this.form.tradeFace + '';
      //   this.open = true;
      //   this.title = "修改产品";
      // });
    },
    // 获取详情
    handleDetails(scope) {
      this.productId = scope.id
      this.detailsStatus = true
      
    },
    /** 提交按钮 */
    submitForm: function () {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.form.id != undefined) {
            updateProductInfo(this.form).then((response) => {
              if (response.code === 200) {
                this.msgSuccess("修改成功");
                this.open = false;
                this.getProductList();
              }
            });
          } else {
            addProductInfo(this.form).then((response) => {
              console.log(response)
              if (response.code === 200) {
                this.msgSuccess("新增成功");
                this.open = false;
                this.getProductList();
              }
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const uploadIds = row.id || this.ids;
      this.$confirm(
        '是否确认删除上传编码为"' + uploadIds + '"的数据项?',
        "警告",
        {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
        }
      )
        .then(function () {
          return deleteData(uploadIds);
        })
        .then(() => {
          this.getProductList();
          this.msgSuccess("删除成功");
        })
        .catch(function () {});
    },
  },
};
</script>